package com.xinke.edu.mypetadoption.fragment.Home;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter4.BaseQuickAdapter;
import com.xinke.edu.mypetadoption.databinding.FragmentHomeBinding;
import com.xinke.edu.mypetadoption.entity.Product;
import com.xinke.edu.mypetadoption.entity.ProductCategory;
import com.xinke.edu.mypetadoption.entity.ResultBody;
import com.xinke.edu.mypetadoption.fragment.Home.adpter.ProductAdpter;
import com.xinke.edu.mypetadoption.fragment.Home.adpter.ProductTypeAdpter;
import com.xinke.edu.mypetadoption.util.GridSpacingItemDecoration;
import com.xinke.edu.mypetadoption.util.RetrofitUtil;

import java.math.BigInteger;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class HomeFragment extends Fragment {
    FragmentHomeBinding binding;
    ProductTypeAdpter adpter;
    ProductAdpter productAdpter;

    int typeId = 0;
    int pageIndex = 1;
    int pageSize = 10;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container ,@Nullable Bundle savedInstanceState){
        binding = FragmentHomeBinding.inflate(inflater);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);
        adpter = new ProductTypeAdpter();
        binding.recyclerview.setAdapter(adpter);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        binding.recyclerview.setLayoutManager(manager);
        getType();
        adpter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener<ProductCategory>() {
            @Override
            public void onClick(@NonNull BaseQuickAdapter<ProductCategory, ?> baseQuickAdapter, @NonNull View view, int i) {
                List<ProductCategory> datas = adpter.getItems();
                for (int a = 0; a < datas.size();a ++){
                    ProductCategory type = datas.get(a);
                    type.setIsselect(i == a);
                    if (i == a){
                        typeId = type.getId();
                        pageIndex = 1;
                        getProduct();
                    }
                }
                adpter.notifyDataSetChanged();
            }
        });

        productAdpter = new ProductAdpter();
        binding.recyclerview2.setAdapter(productAdpter);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(),2);
        binding.recyclerview2.setLayoutManager(gridLayoutManager);
        binding.recyclerview2.addItemDecoration(new GridSpacingItemDecoration(2,10,true,0));
        getProduct();

    }

    public void getProduct(){
        String keySearch = binding.keySearch.getText().toString();
        RetrofitUtil.getApi().getProduct(keySearch,typeId,pageIndex,pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultBody<List<Product>>>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull ResultBody<List<Product>> listResultBody) {
                       productAdpter.addAll(listResultBody.getData());
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    public void getType(){

        RetrofitUtil.getApi().getType()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultBody<List<ProductCategory>>>() {
                    @Override
                    public void onSubscribe(@io.reactivex.rxjava3.annotations.NonNull Disposable d) {
                        
                    }

                    @Override
                    public void onNext(@io.reactivex.rxjava3.annotations.NonNull ResultBody<List<ProductCategory>> listResultBody) {
                        List<ProductCategory> datas = listResultBody.getData();
                        ProductCategory productCategory = new ProductCategory();
                        productCategory.setId(0);
                        productCategory.setName("全部");
                        datas.add(0,productCategory);
                        productCategory.setIsselect(true);
                        adpter.addAll(datas);
                    }

                    @Override
                    public void onError(@io.reactivex.rxjava3.annotations.NonNull Throwable e) {
                        Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}